cross-origin set cookie
한줄교훈
인증정보를 포함할 경우 withCredential을 설정해서 요청을 보내야한다.
이는 서버에서 설정하는 쿠키를 받을 때도 마찬가지이다.
문제
교차출처에서 cookie를 set하는걸 테스트해보는 걸 진행했는데, cookie가 설정되지 않았다.
https://stackoverflow.com/questions/46288437/set-cookies-for-cross-origin-requests
관련 내용을 찾아봤다.
fetch("http://localhost:4000/api").then((res) =>
console.log(res)
);
와 같이 서버에 get요청을 보내서 테스트를 진행했는데 아무리 해도 cookie가 설정되지 않았다.
해결
fetch("http://localhost:4000/api", { credentials: "include" }).then((res) =>
console.log(res)
);
CORS#인증된 요청(Crendtialed Request)
cors시에 인증정보를 포함할 경우 withCredential을 설정해서 요청을 해야하는 건 알았지만, 이는 cookie를 동봉할 때만 필요한 줄 알았다.
쿠키를 설정할 때도 필요하다.